package org.apache.dolphinscheduler.api.controller;

import org.apache.dolphinscheduler.api.page.PageQuery;
import org.apache.dolphinscheduler.api.page.R;
import org.apache.dolphinscheduler.api.page.TableDataInfo;
import org.apache.dolphinscheduler.api.service.RouterService;
import org.apache.dolphinscheduler.dao.entity.Router;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 路由表 前端控制器
 * </p>
 *
 * @author denggh
 * @since 2023-02-08
 */
@RestController
@RequestMapping("/router")
@Api(tags = "路由 前端控制器")
public class RouterController {

    @Autowired
    private RouterService routerService;
    /**
     * 获取路由列表
     */
    @GetMapping("/list")
    @ApiOperation("获取路由列表")
    public TableDataInfo<Router> list(Router router, PageQuery pageQuery) {
        return routerService.selectPageRouterList(router, pageQuery);
    }

    /**
     * 根据路由编号获取详细信息
     *
     * @param routerId 路由ID
     */
    @GetMapping(value = "/{routerId}")
    @ApiOperation("根据路由编号获取详细信息")
    public R<Router> getInfo(@PathVariable Long routerId) {
        return R.ok(routerService.selectRouterById(routerId));
    }

    /**
     * 新增路由
     */
    @PostMapping
    @ApiOperation("新增路由")
    public R<Void> add(@Validated @RequestBody Router router) {

        return R.toAjax(routerService.insertRouter(router));
    }

    /**
     * 修改路由
     */
    @PutMapping
    @ApiOperation("修改路由")
    public R<Void> edit(@Validated @RequestBody Router router) {
        return R.toAjax(routerService.updateRouter(router));
    }

    /**
     * 删除路由
     *
     * @param routerId 路由ID
     */
    @DeleteMapping("/{routerId}")
    @ApiOperation("删除路由")
    public R<Void> remove(@PathVariable Long routerId) {
        return R.toAjax(routerService.deleteRouterById(routerId));
    }
}
